package com.test.spider;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;

import edu.uci.ics.crawler4j.crawler.CrawlConfig;
import edu.uci.ics.crawler4j.crawler.CrawlController;
import edu.uci.ics.crawler4j.fetcher.PageFetcher;
import edu.uci.ics.crawler4j.robotstxt.RobotstxtConfig;
import edu.uci.ics.crawler4j.robotstxt.RobotstxtServer;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class swing {

	private JFrame frame;

	/**
	 * Launch the application.
	 * @throws Exception 
	 */
	public static void main(String[] args) {
		
		

        
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					swing window = new swing();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public swing() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 450, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JButton btnNewButton = new JButton("get");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {

				System.out.println("get mycrawler4j==========>>>>>>>>>>>");

				//爬虫状态存储文件夹，可以从这里边读取数据，以边恢复之前的爬取状态
		        String crawlStorageFolder = "/home/david/mycrawler4j";
		        //爬虫数量，也就是线程数，一般不超过CPU线程数
		        int numberOfCrawlers = 2;
		        //爬虫配置
		        CrawlConfig config = new CrawlConfig();
		        config.setCrawlStorageFolder(crawlStorageFolder);

		        /*
		         * Instantiate the controller for this crawl.
		         */
		        PageFetcher pageFetcher = new PageFetcher(config);
		        RobotstxtConfig robotstxtConfig = new RobotstxtConfig();
		        RobotstxtServer robotstxtServer = new RobotstxtServer(robotstxtConfig, pageFetcher);
		        CrawlController controller = null;
				try {
					controller = new CrawlController(config, pageFetcher, robotstxtServer);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

		        //要爬取的起始地址
		        controller.addSeed("http://www.ics.uci.edu/~lopes/");
		        controller.addSeed("http://www.ics.uci.edu/~welling/");
		        controller.addSeed("http://www.ics.uci.edu/");
		        
				//启动
				controller.start(MyCrawler.class, numberOfCrawlers);
			}
		});
		
		JButton btnNewButton_1 = new JButton("New button");
		GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.TRAILING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(53)
					.addComponent(btnNewButton_1)
					.addPreferredGap(ComponentPlacement.RELATED, 163, Short.MAX_VALUE)
					.addComponent(btnNewButton)
					.addGap(38))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.TRAILING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap(227, Short.MAX_VALUE)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnNewButton)
						.addComponent(btnNewButton_1))
					.addGap(22))
		);
		frame.getContentPane().setLayout(groupLayout);
	}
}
